package com.example.myapplication.bing

import com.example.myapplication.Constants
import com.example.myapplication.util.HttpUtils
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive


/**
 * @author Niu
 * @date 2025-08-17
 */
class BingWallpaper {

    /*
     *{
    "f": "raf",
    "v": "1.0",
    "rdr":
    [
        {
            "c": "CDMLite",
            "u": "DesktopSpotlightSurface"
        }
    ],
    "ad":
    {
        "landscapeImage":
        {
            "asset": "https://res.public.onecdn.static.microsoft/creativeservice/0b28ee45-aa7c-8b9c-90d7-c54de2811551_desktop-b011_ds_adeliepauletteisland_gettyimages-971578238_3840x2160.jpg"
        },
        "portraitImage":
        {
            "asset": "https://res.public.onecdn.static.microsoft/creativeservice/ce775a59-daa5-d40c-b6e3-b6cdb70b959f_desktop-b011_ds_adeliepauletteisland_gettyimages-971578238_1080x1920.jpg"
        },
        "iconLabel": "了解有关此图片的信息",
        "iconHoverText": "南极洲，在保利特岛潜水的阿德利企鹅\r\n© KeithSzafranski / E+ / Getty Images\r\n点击鼠标右键以了解更多信息",
        "title": "圆胖的潜水员",
        "description": "阿德利企鹅潜水小队只是保利特岛企鹅种群的一小部分。这片位于南极半岛之外的陆地和冰层是成千上万迷人且好奇的生物的家园。尽管身材娇小（大约 76 厘米高），但阿德利企鹅还是以无所畏惧而著称。从早期的南极探险家到现代的游轮乘客，都会欣喜地看到勇敢的企鹅群冲上前来向他们打招呼。在充满艰难险阻的斯科特探险期间，英国编年史学家阿普斯利·切里-加勒德成为了阿德利企鹅的爱好者，后来他亲切地将这种鸟描述为“一位近乎于人类的朋友”。",
        "copyright": "© KeithSzafranski / E+ / Getty Images",
        "likeGlyph": "",
        "dislikeGlyph": "",
        "ctaText": "了解详情",
        "ctaUri": "microsoft-edge:https://www.bing.com/spotlight?spotlightid=DS_AdeliePauletteIsland&q=阿德利企鹅&FORM=MC13ER&setlang=zh-cn&mkt=zh-cn",
        "relatedContent":
        [
            {
                "glyph": "",
                "label": "查看更多照片",
                "actionUri": "microsoft-edge:https://cn.bing.com/images/search?q=阿德利企鹅&FORM=MC13ET&ensearch=0&setlang=zh-cn&mkt=zh-cn"
            },
            {
                "glyph": "",
                "label": "企鹅的种类",
                "actionUri": "microsoft-edge:https://cn.bing.com/search?q=企鹅+种类&FORM=MC13EU&ensearch=0&setlang=zh-cn&mkt=zh-cn"
            },
            {
                "glyph": "",
                "label": "探索南极洲",
                "actionUri": "microsoft-edge:https://cn.bing.com/travel/place-information?q=南极洲&SID=ea024ba1-aafe-46ac-b588-b3d7c6da04a5&FORM=MC13EV&ensearch=0&setlang=zh-cn&mkt=zh-cn"
            }
        ],
        "relatedHotspots":
        [
            {
                "glyph": "",
                "label": "这张南极照片中所有迷人而傻乎乎的小家伙曾经都是像这样的小绒球。",
                "actionUri": "microsoft-edge:https://cn.bing.com/images/search?q=幼崽+阿德利企鹅&FORM=MC13EW&ensearch=0&setlang=zh-cn&mkt=zh-cn"
            },
            {
                "glyph": "",
                "label": "自人类首次远征南极以来，可爱而好奇的阿德利企鹅就一直吸引着人类的目光。",
                "actionUri": "microsoft-edge:https://cn.bing.com/spotlight?spotlightid=DS_AdeliePauletteIsland&q=阿德利企鹅&form=MC13EX&ensearch=0&setlang=zh-cn&mkt=zh-cn"
            }
        ],
        "entityId": "128000000004946429"
    },
    "tracking":
    {
        "baseUri": "https://ris.api.iris.microsoft.com/v1/a/{ACTION}?PG=IRIS000001.0000000820&UNID=88000820&CID=128000000004946429&PID=426010594&TargetID=1&REQASID=&ASID=A051C0A3001C4C4FACCBCCFD9C6CF40B&REQT=20250817T062724&UIT=M&ID=00000000000000000000000000000001&region=CN&SLOT=1&RV=&RS=&ER_AC=&&DS_EVTID=A051C0A3001C4C4FACCBCCFD9C6CF40B&WFIDS=&QFIDS=&DEVOSVER=&APP=&ARCRAS=&CLR=&IRIS_EXP_FLIGHTS=&lang=&oem=&devFam=&ossku=&cmdVer=&mo=&cap=&bSrc=i.m"
    },
    "prm":
    {
        "_id": "128000000004946429",
        "_imp": "https://arc.msn.com/v3/Delivery/Events/Impression?PID=426010594&TID=1&CID=128000000004946429&BID=1499381590&PG=IRIS000001.0000000820&TPID=426010594&REQASID=&ASID=A051C0A3001C4C4FACCBCCFD9C6CF40B&SLOT=1&REQT=20250817T062724&MA_Score=0&&DS_EVTID=A051C0A3001C4C4FACCBCCFD9C6CF40B&PG=IRIS000001.0000000820&UNID=88000820&MAP_TID=AEEA27DE-E475-4B81-8BFF-EFA60D9D2961&ASID=A051C0A3001C4C4FACCBCCFD9C6CF40B&ARC=1&EMS=1&LOCALE=ZH-CN&COUNTRY=CN&HTD=-1&LANG=2052&DEVLANG=ZH&CIP=58.211.155.154&OPTOUTSTATE=0&NGPAGEGROUPOPTOUT=0&HTTPS=1&MARKETBASEDCOUNTRY=CN&CFMT=&H=0&W=0&FESVER=1.3&IEPE=1&CHNL=CFD&IRIS_EXP_FLIGHTS=",
        "_flight": ""
    }
}
     */
    // 获取每日壁纸信息
    fun getDailyWallpaperInfo(): WallpaperInfo {
        val data = HttpUtils.get(Constants.DAILY_URL)
        println(data);
        // 解析对象
        val jsonObject = Json.parseToJsonElement( data).jsonObject
        // 解析信息
        val ad = jsonObject["ad"]!!.jsonObject
        val landscapeImageUrl = ad["landscapeImage"]!!.jsonObject["asset"]!!.jsonPrimitive.content
        val portraitImageUrl = ad["portraitImage"]!!.jsonObject["asset"]!!.jsonPrimitive.content
        val title = ad["title"]?.jsonPrimitive?.content ?: "无标题"
        val description = ad["description"]?.jsonPrimitive?.content ?: "无描述"
        val copyright = ad["copyright"]?.jsonPrimitive?.content ?: "无版权"
        return WallpaperInfo(landscapeImageUrl, portraitImageUrl, title, description, copyright)
    }
}